#ifndef _STM32F10x_FLASH_H
#define _STM32F10x_FLASH_H

#include "stm32f10x_flash.h"
#include "main.h"

#include "stm32f10x.h"

FLASH_Status ErasePageDataFlash(uint32_t address)
{
	FLASH_Status status;
	FLASH_Unlock();
	FLASH_ClearFlag(FLASH_FLAG_EOP | FLASH_FLAG_PGERR | FLASH_FLAG_WRPRTERR);
	status = FLASH_ErasePage(address);
	status = FLASH_WaitForLastBank1Operation(10);
	FLASH_Lock();
	return status;
}

FLASH_Status WriteDataFlash(uint32_t address,uint32_t* data,uint32_t NumHalfWord )
{
	uint32_t j;
	FLASH_Status st;
	FLASH_Unlock();
	FLASH_ClearFlag(FLASH_FLAG_EOP | FLASH_FLAG_PGERR | FLASH_FLAG_WRPRTERR);
	for(j=0;j<NumHalfWord;j++)
		{
			st=FLASH_ProgramWord(address, *data++);
			st = FLASH_WaitForLastBank1Operation(10);
			if(st!=FLASH_COMPLETE){return st;};
				address+=4;
		};
		FLASH_Lock();
	return FLASH_COMPLETE;
}

uint32_t Read_flash (uint32_t adr)
{
 uint32_t a;
 a = *(__IO uint32_t*) adr;
 return a;
}

FLASH_Status ReadDataFlash(uint32_t address,uint32_t* data,uint32_t NumHalfWord )
{
	
	uint32_t j;
	FLASH_Unlock();
	for(j=0;j<NumHalfWord;j++)
		{
			*data++=Read_flash(address);
			address+=4;
		};
		FLASH_Lock();
	return FLASH_COMPLETE;
	
}

#endif
